
Ext.onReady(function() {
	Ext.define('Spec', {
        extend: 'Ext.data.Model',
        fields: [
            { name: 'QUERYID', type: 'string'},
            { name: 'ID', type: 'string'},
            { name: 'SPENDTYPE', type: 'string'},
            { name: 'MONEY', type: 'string'},
            { name: 'ITEMS', type: 'string'},
            { name: 'ACTIONDATE', type: 'date'}
        ]
    });
	
	var store = Ext.create('Ext.data.Store',{
		pageSize : 30,
		model : 'Spec'
	});
	
	var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
        clicksToMoveEditor: 1,
//        clicksToEdit: 1,
        autoCancel: true,
        listeners:{
        	edit:{
        		fn:gridEdit
        	}
        }
    });
	
	var startDate = Ext.create('Ext.form.field.Date', {
		xtype: 'datefield',
        fieldLabel: '起始日期',
        editable: false
	});
	
	var endDate = Ext.create('Ext.form.field.Date',{
		xtype: 'datefield',
        fieldLabel: '终止日期',
        editable: false
	});
	
	var itemStore = Ext.create('Ext.data.Store',{
		fields: [{name: 'ITEM', type:'string'}]
	});
	
	var grid = Ext.create('Ext.grid.Panel', {
		title: '条目',
		region: 'center',
		split: true,
		store : store,
		loadMask : true,
		multiSelect : true,
		columns: [
          {
              xtype: 'rownumberer',
              width: 30
          },
          {
              xtype: 'gridcolumn',
              hidden: true,
              width: 0,
              defaultWidth: 0,
              dataIndex: 'ID',
              hideable: false,
              text: 'ID',
              editor : {
            	  
              }
          },
          {
        	  xtype: 'gridcolumn',
        	  width: 100,
        	  dataIndex: 'SPENDTYPE',
        	  hideable: false,
        	  text: '消费类型',
        	  editor : {
        		  xtype: 'combobox',
                  typeAhead: true,
                  triggerAction: 'all',
                  selectOnTab: true,
                  displayField: 'SPENDTYPE',
                  valueField: 'SPENDTYPE',
        		  store: {
    			    fields: [
    			            { name: 'SPENDTYPE', type: 'string'}
    			        ],
        			 proxy:{
        				type: 'ajax',
    					url : getPath("/ab/po/houseKeeper.do"),
						extraParams : {
			            	"md":"readcode",
			            	"type":"spendtype"
			            },
						reader : {
							type : 'json',
							root : 'spendtype'
						}
        			 }
        		  },
                  lazyRender: true,
                  listeners:{
                	  select : function(combo, record,index) {
                		  itemStore.setProxy({
                				type : 'ajax',
                				url : getPath("/ab/po/houseKeeper.do"),
                				extraParams : {
                	            	"md":"readcode",
                	            	"type":"spendtypeitem",
                	            	"spendtype":combo.value
                	            },
                				reader : {
                					type : 'json',
                					root : 'spendtypeitem',
                					totalProperty : 'totalCount'
                				}
                		  });
                		  itemStore.load();
                	  }
                  }
        	  }
          },
          {
              xtype: 'gridcolumn',
              width: 241,
              dataIndex: 'ITEMS',
              flex: 1,
              hideable: false,
              text: '项目',
              editor : {
            	  xtype: 'combobox',
                  typeAhead: true,
                  triggerAction: 'all',
                  selectOnTab: true,
                  displayField: 'ITEM',
                  valueField: 'ITEM',
        		  store: itemStore,
        		  multiSelect: true,
                  lazyRender: true
        	  }
          },
          {
              xtype: 'gridcolumn',
              width: 297,
              dataIndex: 'MONEY',
              hideable: false,
              text: '金额',
              editor : {
            	  
              }
          },
          {
              xtype: 'datecolumn',
              width: 131,
              dataIndex: 'ACTIONDATE',
              hideable: false,
              text: '日期',
              format : 'Y-m-d',
              editor : {
            	  xtype: 'datefield',
            	  format:'Y-m-d'
              }
          },
          {
              xtype: 'actioncolumn',
              draggable: false,
              sortable: false,
              hideable: false,
              items: [
                  {
                	  icon:getPath('/common/images/delete.png'),
                	  handler : function(grid, rowIndex, colIndex){
                		  var id = store.getAt(rowIndex).get('ID');
                		  if (id != null && id != "") {
                			  Ext.MessageBox.confirm("确认","是否要删除？", function(button, text) {
                				  if(button == "yes") {
                					  Ext.Ajax.request({
            							  url : getPath('/ab/po/houseKeeper.do'),
            							  params : {
            								  md : 'd',
            								  ID : [id]
            							  }
            						  });
                					  rowEditing.cancelEdit();
                					  var sm = grid.getSelectionModel();
                					  sm.select(rowIndex);
                					  store.remove(sm.getSelection());
                				  }
                			  });
                		  }
                		  else {
                			  rowEditing.cancelEdit();
        					  var sm = grid.getSelectionModel();
        					  sm.select(rowIndex);
        					  store.remove(sm.getSelection());
                		  }
                	  }
                  }
              ]
          }
        ],
        dockedItems: [
          {
              xtype: 'toolbar',
              dock: 'top',
              items: [
                  {
                      xtype: 'button',
                      iconCls:'icon-add',
                      text: '添加',
                      listeners: {
                          click: {
                        	  fn:addRow
                          }
                      }
                  },
                  {
                      xtype: 'tbseparator'
                  },
                  startDate
                  ,
                  endDate,
                  {
                      xtype: 'button',
                      text: '查询',
                      listeners: {
                          click: {
                              fn: query
                          }
                      }
                  }
              ]
          },
          {
              xtype: 'pagingtoolbar',
              displayInfo: true,
              store: store,
              dock: 'bottom'
          }
        ],
        plugins: [rowEditing],
        height:600
	});
	
	function addRow() {
		rowEditing.cancelEdit();
		var r = Ext.create('Spec', {
			ID: '',
			MONEY: '',
			ITEMS : '',
			ACTIONDATE: Ext.Date.format(new Date(), 'Y-m-d')
        });
		store.insert(0, r);
        rowEditing.startEdit(0, 0);
	}
	
	function query(){
		store.setProxy({
			type : 'ajax',
			url : getPath("/ab/po/houseKeeper.do"),
			extraParams : {
            	"md":"r",
            	"STARTDATE":startDate.getRawValue(),
            	"ENDDATE":endDate.getRawValue()
            },
			reader : {
				type : 'json',
				root : 'housekeeper',
				totalProperty : 'totalCount'
			}
		});
		store.loadPage(1);
	}
	
	function gridEdit(editor, e) {
        Ext.Ajax.request({
            url : getPath("/ab/po/houseKeeper.do"),  
            params : {
            	"md":"u",
                "ID" : e.record.data['ID'],
                "SPENDTYPE" : e.record.data['SPENDTYPE'],
                "MONEY" : e.record.data['MONEY'],
                "ITEMS" : e.record.data['ITEMS'],
                "ACTIONDATE" : e.record.data['ACTIONDATE']
            },
            success : function(response) {  
            	var ret = Ext.JSON.decode(response.responseText);
            	if(ret.success){
                    e.record.data['ID'] = ret.msg;
                    e.record.commit();
            	}
            	else {
            		Ext.MessageBox.alert("错误",ret.msg);
            	}
            }
        });
	}
	
	var view = Ext.create('Ext.container.Viewport', {
		layout : 'auto',
		renderTo : Ext.getBody(),
		items : [grid]
	});
});